const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');

Page({

	data: {
		userInfo: {
			avatar: '/image/headimg.png',
			realname: '刘小凤'
		},
		checkedIn: false,
		attendTime: "", //打卡时间
		checkaddrIn: false, //校验是否到打卡范围
		addrNum: ""
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		var coursename = options.coursename; //课程名称
		var time = options.time; //打卡时间[当天时间]
		var cid = options.cid; //课程id
		var ctime = options.ctime; //上课时间段
		var coursearrange_id = options.coursearrange_id; //课节id
		var userInfo = app.globalData.userInfo;
		this.setData({
			coursename: coursename,
			time: time,
			cid: cid,
			ctime: ctime,
			coursearrange_id: coursearrange_id,
			userInfo: userInfo
		});


		this.checkAttend();
		// this.checkAddr();  //fixme 关闭位置校验
	},
	checkAttend() { //校验是否打卡过
		var that = this;
		http.httpPost(config.checkAttend, {
			time: that.data.time,
			curriculum_id: that.data.cid,
			coursearrange_id: that.data.coursearrange_id
		}, function(e) {
			if (e.code == 1) {
				if (e.data != "") {
					that.setData({
						checkedIn: true,
						attendTime: e.data
					});
				}
			} else {

			}

		});
  },
  goStudentAttend(){//去点名
      var time = this.data.time;
      var cid = this.data.cid;
      var coursearrange_id = this.data.coursearrange_id;

      wx.navigateTo({
        url: '/teacher/roll_call/details?cid='+cid+'&time='+time+'&coursearrange_id='+coursearrange_id
      })
  },
	goAddrIn() {
		wx.showToast({
			title: "超出打卡范围",
			icon: 'none',
			duration: 3000,
			mask: true
		});
	},
	checkAddr() { //校验位置
		//获取用户地理位置
		//先判断用户是否授权获取地理位置
		let that = this;
		wx.getLocation({
			type: 'gcj02', //wgs84为GPS全球定位,可用gcj02(中国国家测绘局)，精度高些
			success(res) {
				const latitude = res.latitude
				const longitude = res.longitude
				const speed = res.speed
				const accuracy = res.accuracy
				// console.log(res);
				that.checkAttendAddr(longitude, latitude); //校验位置
			},
			fail(res) {
				console.log(res);
			}

		})
		var ii = 1;
	},
	checkAttendAddr(longitude, latitude) { //校验是否在打卡范围
		var that = this;
		http.httpPost(config.checkAttendAddr, {
			longitude: longitude,
			latitude: latitude
		}, function(e) {
			console.log(e);
			if (e.code == 1) {
				that.setData({
					checkaddrIn: true,
					// addrNum:e.data
				});
			} else {
				wx.showToast({
					title: e.msg,
					icon: 'none',
					duration: 3000,
					mask: true
				});
			}

		});
	},
	downGoBtn() { //取消打卡btn
		var that = this;
		http.httpPost(config.downAttend, {
			time: that.data.time,
			curriculum_id: that.data.cid,
			coursearrange_id: that.data.coursearrange_id
		}, function(e) {
			if (e.code == 1) {
				that.setData({
					checkedIn: false,
					// attendTime: e.data
				});
				wx.showToast({
					title: e.msg,
					icon: 'none',
					duration: 3000,
					mask: true
				});
			} else {
				wx.showToast({
					title: e.msg,
					icon: 'none',
					duration: 3000,
					mask: true
				});
			}

		});

	},
	goAttend() { //打卡btn
		// if (this.data.checkaddrIn == false) {  //fixme 关闭位置判断
		// 	this.goAddrIn();
		// 	return false;
		// }

		var that = this;
		http.httpPost(config.goAttend, {
			time: that.data.time,
			curriculum_id: that.data.cid,
			coursearrange_id: that.data.coursearrange_id
		}, function(e) {
			if (e.code == 1) {

				that.setData({
					checkedIn: true,
					attendTime: e.data
				});
				wx.showToast({
					title: e.msg,
					icon: 'none',
					duration: 3000,
					mask: true
				});
			} else {
				wx.showToast({
					title: e.msg,
					icon: 'none',
					duration: 3000,
					mask: true
				});
			}

		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {
		if (!app.login()) {
			return;
		}
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
